﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionClone.View;
using System.IO;

namespace SolutionClone.Presenter
{
    static class SolutionClonerPresenter
    {
        public static void Clone(this ISolutionClonerView view)
        {
            view.Running = true;
            try
            {
                var model = new Model.SolutionCloner();
                model.SourceDirectory = view.OldPath;
                model.DestinationDirectory = view.NewPath;
                model.OldSolutionName = view.OldName;
                model.NewSolutionName = view.NewName;
                model.Clone();
            }
            finally
            {
                view.Running = false;
            }
        }

        public static void ValidateOldPath(this IInputValidator view)
        {
            if (!Directory.Exists(view.OldPath))
            {
                view.OldPathValid = false;
                view.ErrorText = "Directory does not exist.";
            }
            else
            {
                view.OldPathValid = true;
                view.ErrorText = string.Empty;
            }
        }

        public static void ValidateNewPath(this IInputValidator view)
        {
            if (Directory.Exists(view.NewPath))
            {
                view.NewPathValid = false;
                view.ErrorText = "Directory already exists.";
            }
            else
            {
                view.NewPathValid = true;
                view.ErrorText = string.Empty;
            }
        }

        public static void ValidateOldName(this IInputValidator view)
        {
            bool valid = view.OldPath.Contains(view.OldName);
            view.OldNameValid = valid;
            if (valid)
                view.ErrorText = string.Empty;
            else view.ErrorText = "Source Path must contain the source name.";
        }

        public static void ValidateNewName(this IInputValidator view)
        {
            if (view.NewName == view.OldName)
            {
                view.NewNameValid = false;
                view.ErrorText = "Cannot equal Source Name.";
            }
            else
            {
                view.NewNameValid = true;
                view.ErrorText = string.Empty;
            }
        }
    }
}
